<!DOCTYPE html>
<html lang="zh-CH" >
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="author" content="" />
	
	
	
	<title>Unity3D 动态生成二维码 ｜ zansimple</title>
	
    
    
    <meta name="description" content="前一段时间开发项目中需要用到二维码识别登录，项目做完之后没来得及整理 所有现在先整理一下希望对你们有所帮助
准备工作 需要下载个工具 &amp;ndash; 链接 密码：80bg 将下载下来的文件放到你的Unity工程里面 （整理好的）
生成二维码 using UnityEngine; using ZXing; using ZXing.QrCode; using System.IO; using System.Collections.Generic;
namespace Tools.Code2D{public class BuildCode2D : Singleton&amp;lt;BuildCode2D&amp;gt;{public Sprite CreateSpriteCode2D(string code_2d_message){Texture2D code_2d = CreateTextureCode2D(code_2d_message);return Sprite.Create(code_2d, new Rect(0, 0, code_2d.width, code_2d.height), Vector2.zero);}public Texture2D CreateTextureCode2D(string code_2d_message){Texture2D code_2d = new Texture2D(256, 256);if (code_2d_message != null){var color32 = Encode(code_2d_message, code_2d.width, code_2d.height);code_2d.SetPixels32(color32);code_2d." />
    

    

	
    
    <link rel="shortcut icon" href="https://lenzan.github.io/images/favicon.ico" />

    <link rel="stylesheet" type="text/css" media="screen" href="https://lenzan.github.io/css/normalize.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://lenzan.github.io/css/zozo.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="https://lenzan.github.io/css/highlight.css" />

    
    
</head>

<body>
    <div class="main animate__animated animate__fadeInDown">
        <div class="nav_container animated fadeInDown">
    <div class="site_nav" id="site_nav">
        <ul>
            
            <li>
                <a href="/">首页</a>
            </li>
            
            <li>
                <a href="/">unity</a>
            </li>
            
            <li>
                <a href="/">归档</a>
            </li>
            
            <li>
                <a href="/">关于</a>
            </li>
            
        </ul>
    </div>
    <div class="menu_icon">
        <a id="menu_icon"><i class="ri-menu-line"></i></a>
    </div>
</div>
        <div class="header animated fadeInDown">
    <div class="site_title_container">
        <div class="site_title">
            <h1>
                <a href="https://lenzan.github.io/">
                    <span>zansimple</span>
                    <img src="https://lenzan.github.io/images/logo.svg" />
                </a>
            </h1>
        </div>
        <div class="description">
            <p class="sub_title">专注于Unity虚拟现实开发，游戏开发</p>
            <div class="my_socials">
                
                <a href="https://lenzan.github.io/index.xml" type="application/rss+xml" title="rss" target="_blank"><i
                        class="ri-rss-fill"></i></a>
            </div>
        </div>
    </div>
</div>
        <div class="content">
            <div class="post_page">
                <div class="post animate__animated animate__fadeInDown">
                    <div class="post_title post_detail_title">
                        <h2><a href='/posts/Unity/buildCode2D/'>Unity3D 动态生成二维码</a></h2>
                        <span class="date">2018.05.06</span>
                    </div>
                    <div class="post_content markdown"><!-- raw HTML omitted -->
<p>前一段时间开发项目中需要用到二维码识别登录，项目做完之后没来得及整理 所有现在先整理一下希望对你们有所帮助</p>
<h2 id="准备工作">准备工作</h2>
<p>需要下载个工具 &ndash;  <a href="http://pan.baidu.com/s/1nvsogUt">链接</a> 密码：80bg
将下载下来的文件放到你的Unity工程里面 （整理好的）</p>
<h2 id="生成二维码">生成二维码</h2>
<p>using UnityEngine;
using ZXing;
using ZXing.QrCode;
using System.IO;
using System.Collections.Generic;</p>
<pre><code>namespace Tools.Code2D
{
    public class BuildCode2D : Singleton&lt;BuildCode2D&gt;
    {
        public Sprite CreateSpriteCode2D(string code_2d_message)
        {
            Texture2D code_2d = CreateTextureCode2D(code_2d_message);
            return Sprite.Create(code_2d, new Rect(0, 0, code_2d.width, code_2d.height), Vector2.zero);
        }
        public Texture2D CreateTextureCode2D(string code_2d_message)
        {
            Texture2D code_2d = new Texture2D(256, 256);
            if (code_2d_message != null)
            {
                var color32 = Encode(code_2d_message, code_2d.width, code_2d.height);
                code_2d.SetPixels32(color32);
                code_2d.Apply();
                byte[] bytes = code_2d.EncodeToPNG();//把二维码转成byte数组，然后进行输出保存为png图片就可以保存下来生成好的二维码  
                if (!Directory.Exists(Application.dataPath + &quot;/Code2D&quot;))//创建生成目录，如果不存在则创建目录  
                {
                    Directory.CreateDirectory(Application.dataPath + &quot;/Code2D&quot;);
                }
                string fileName = Application.dataPath + &quot;/Code2D/&quot; + code_2d_message + &quot;.png&quot;;
                File.WriteAllBytes(fileName, bytes);
            }
            return code_2d;
        }
        private static Color32[] Encode(string textForEncoding, int width, int height)
        {
            Dictionary&lt;EncodeHintType, object&gt; hints = new Dictionary&lt;EncodeHintType, object&gt;();
            hints.Add(EncodeHintType.CHARACTER_SET, &quot;UTF-8&quot;);
            var writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width = width,
                },
                Encoder = new QRCodeWriter
                {

                }
            };
            var bitMatrix = writer.Encoder.encode(textForEncoding, BarcodeFormat.QR_CODE, width, height, hints);
            return writer.Write(bitMatrix);
        }
    }
}
</code></pre>
<h2 id="创建脚本createcode2d">创建脚本CreateCode2D</h2>
<pre><code>using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Tools.Code2D;
using System;

public class CreateCode2D : MonoBehaviour {
    public Image image;
    //创建二维码
    public Sprite CreateSpriteCode()
    {
       return  BuildCode2D.Instance.CreateSpriteCode2D(GetTimeStamp());
    }

    public Texture2D CreatTexture2DCode()
    {
        return BuildCode2D.Instance.CreateTextureCode2D(GetTimeStamp());
    }
    //获取当前的时间戳
    public string GetTimeStamp()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        string result = ts.TotalSeconds.ToString();
        return result;
    }

    //点击生成二维码的按钮
    public void ButtonClick()
    {
        image.sprite = CreateSpriteCode();
    }
}
</code></pre>
<p>此脚本只是用来生成二维码（根据当前的时间戳） 因为时间原因就先写到这里了，后面会将二维码的识别功能也加上。</p>
</div>
                    <div class="post_footer">
                        
                        <div class="meta">
                            <div class="info">
                                <span class="field tags">
                                    <i class="ri-stack-line"></i>
                                    
                                    <a href="https://lenzan.github.io/tags/Unity3D/">Unity3D</a>
                                    
                                    <a href="https://lenzan.github.io/tags/AssetBundle/">AssetBundle</a>
                                    
                                </span>
                            </div>
                        </div>
                        
                    </div>
                </div>
                
                
            </div>
        </div>
    </div>
    <a id="back_to_top" href="#" class="back_to_top"><span>△</span></a>
    <footer class="footer">
    <div class="powered_by">
        <a href="https://varkai.com">Designed by VarKai,</a>
        <a href="http://www.gohugo.io/">Proudly published with Hugo</a>
    </div>

    <div class="footer_slogan">
        <span></span>
    </div>
</footer>
    <script src="https://lenzan.github.io/js/jquery-3.5.1.min.js"></script>
<link href="https://lenzan.github.io/css/fancybox.min.css" rel="stylesheet">
<script src="https://lenzan.github.io/js/fancybox.min.js"></script>
<script src="https://lenzan.github.io/js/zozo.js"></script>






</body>

</html>